package servlet;

import bean.Result;
import biz.userBiz;
import com.google.gson.Gson;
import dao.UserLoginDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/UserLogin.s")
public class UserLoginServlet extends HttpServlet {

    private userBiz loginr_mBiz = new userBiz();
    UserLoginDao dao=new UserLoginDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final HttpSession session = req.getSession();
        final Object vcode = session.getAttribute("vcode");
        final String yzm = req.getParameter("yzm");
        Result result = null;      final String username = req.getParameter("username");
        final String pass = req.getParameter("pass");
        if (username==null||username.trim().isEmpty()){
            result =new Result(0,"用户名不能为空",null);
        }
        if (pass==null||pass.trim().isEmpty()){
            result =new Result(0,"密码不能为空",null);

        }
        if (yzm==null||yzm.trim().isEmpty()){
            result =new Result(0,"请输入验证码",null);

        }
        if (((String)vcode).equalsIgnoreCase( yzm)){
            Map<String,Object> map=new HashMap<>();
            System.out.println(map);
            if (username.trim().isEmpty()==false&&pass.trim().isEmpty()==false){
                map=loginr_mBiz.login(username,pass);
                System.out.println(map);
                if (map!=null){
                    result = new Result(1, "登录成功", null);
                    final Map<String, Object> queryid = dao.queryid(username);
                    final Object uid = queryid.get("id");
                    session.setAttribute("user",username);
                    session.setAttribute("userid",uid);
                    final Object requestURI = session.getAttribute("requesturl");
                    final Object requesturl_per = session.getAttribute("requesturl_per");
                    if (requestURI!=null){
                        result.setData(requestURI);
                    }
                    if (requesturl_per!=null){
                        result.setData(requesturl_per);
                    }
                    System.out.println(requestURI);
                    System.out.println(requesturl_per);
                }else {
                    result = new Result(0, "用户名或者密码错误", null);
                }
            }
        }else {
            result=new Result(0,"验证码输入错误!",null);
        }
        final String json = new Gson().toJson(result);
        resp.getWriter().append(json);
    }
}
